package initialize

import (
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver"
	"google.golang.org/grpc"
	"log"
	"userop_bff/global"
	"userop_bff/goods_proto"
	"userop_bff/userop_proto"
)

func InitSrvConnect() {
	//商品srv
	GoodsConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", global.ServerConfig.ConsulConfig.ConsulHost, global.ServerConfig.ConsulConfig.ConsulPort, global.ServerConfig.ConsulConfig.GoodsSrvName),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		log.Fatal(err)
	}
	global.GoodsSrvClient = goods_proto.NewGoodsClient(GoodsConn)

	//用户中心srv
	UseropConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", global.ServerConfig.ConsulConfig.ConsulHost, global.ServerConfig.ConsulConfig.ConsulPort, global.ServerConfig.ConsulConfig.UseropSrvName),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		log.Fatal(err)
	}
	global.AddressSrvClient = userop_proto.NewAddressClient(UseropConn)
	global.MessageSrvClient = userop_proto.NewMessageClient(UseropConn)
	global.UserFavSrvClient = userop_proto.NewUserFavClient(UseropConn)
}
